package com.beiing.tianshuai.tianshuai.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.UploadImgBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.google.gson.Gson;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by Jinni on 2017/11/7 0007
 */

public class DiscoveryPublishModel implements DiscoveryPublishModelImpl {

    private static final String TAG = "DiscoveryPublishModel";
    private OnResponseListener mListener;

    public DiscoveryPublishModel(OnResponseListener listener) {
        mListener = listener;
    }

    @Override
    public void UploadImages(List<File> images) {
        // 构建body
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (images.size() > 1) {
            // 多张图时创建文件数据进行批量上传
            for (File image : images) {
                builder.addFormDataPart("freshcover[]", image.getName(), RequestBody.create(MediaType.parse("image/*"), image));
            }
        } else {
            // 单张图的上传方式
            File file = images.get(0);
            builder.addFormDataPart("freshcover", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
        }
        RequestBody requestBody = builder.build();
        Observable<ResponseBody> observable = DataManager.getUploadImgResult(
                requestBody
        );
        Observable<ResponseBody> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<ResponseBody>() {

            @Override
            public void onNext(@NonNull ResponseBody bean) {
                if (mListener != null) {
                    try {
                        String result = bean.string();
                        Gson gson = new Gson();
                        UploadImgBean resultBean = gson.fromJson(result, UploadImgBean.class);
                        mListener.onUploadSuccess(resultBean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getPublishResult(String content, String uid, String title, String freshCover) {
        Observable<CodeBean> observable = DataManager.getDiscoveryPublishResult(
                content,
                uid,
                title,
                freshCover
        );
        Observable<CodeBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<CodeBean>() {

            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnResponseListener {
        void onSuccess(CodeBean bean);
        void onUploadSuccess(UploadImgBean bean);
        void onError(Throwable throwable);
    }
}
